package com.zyd.shiro.business.service;

import java.io.UnsupportedEncodingException;
import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.zyd.shiro.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.zyd.shiro.persistence.dao.MallUserExamineDao;
import com.zyd.shiro.persistence.beans.UserExamine;

/**
 * 服务层
 * 
 * @author RookieWZW
 *
 */
@Service
public class MallUserExamineService {

	@Autowired
	private MallUserExamineDao userExamineDao;
	
	@Autowired
	private QiniuUtil qiniuUtil;

	public List<UserExamine> findAll() {

		return userExamineDao.findAll();
	}

	public List<UserExamine> findAllByState(Integer state){

		return userExamineDao.findAllByState(state);
	}
	/**
	 * 从七牛云获得图片
	 */
	public String downloadTest(String filename) throws UnsupportedEncodingException {
		return qiniuUtil.getFile(filename);
	}

	/**
	 * 更新审核状态
	 * @param state
	 * @param id
	 */
	public void updateState(Integer state,String re,Integer id){
		userExamineDao.updateState(state,re,id);
	}

	public List<UserExamine> findAllByUser_id(Integer user_id){
		return userExamineDao.findAllByUser_id(user_id);
	}



	private Specification<UserExamine> where(Map searchMap) {
		
		return new Specification<UserExamine>() {
          
			@Override
			public Predicate toPredicate(Root<UserExamine> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 审核图片
                if (searchMap.get("image")!=null && !"".equals(searchMap.get("image"))) {
                	predicateList.add(cb.like(root.get("image").as(String.class), "%"+(String)searchMap.get("image")+"%"));
                }
                // 描述
                if (searchMap.get("description")!=null && !"".equals(searchMap.get("description"))) {
                	predicateList.add(cb.like(root.get("description").as(String.class), "%"+(String)searchMap.get("description")+"%"));
                }
                // 审核失败原因
                if (searchMap.get("reasion")!=null && !"".equals(searchMap.get("reasion"))) {
                	predicateList.add(cb.like(root.get("reasion").as(String.class), "%"+(String)searchMap.get("reasion")+"%"));
                }

				if (searchMap.get("userId")!=null && !"".equals(searchMap.get("userId"))) {
					predicateList.add(cb.equal(root.get("userId").as(Integer.class), (Integer)searchMap.get("userId")));
				}

				if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
					predicateList.add(cb.equal(root.get("state").as(Integer.class), (Integer)searchMap.get("state")));
				}


                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
                
            }
        };		
		
	}

	public Page<UserExamine> findSearch(int whereString, int page, int size) {
		Map whereMap = new HashMap<>();
		whereMap.put("userId",whereString);
		Specification<UserExamine> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return userExamineDao.findAll(specification, pageRequest);
	}
	/**
	 * 分页查询
	 *
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<UserExamine> findPage(int page, int size,int type) {
		Map whereMap = new HashMap<>();
		whereMap.put("state",type);
		Specification<UserExamine> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return userExamineDao.findAll(specification, pageRequest);
	}

	public UserExamine findOne(Integer id) {
		return userExamineDao.findOne(id);
	}

	public void add(UserExamine userExamine) {
		
		userExamineDao.save(userExamine);
	}
	
	public void update(UserExamine userExamine) {
		userExamineDao.save(userExamine);
	}

	public void delete(Integer id) {
		userExamineDao.delete(id);
	}

	public void deleteList(Integer[] ids) {
		for (Integer id : ids) {
			userExamineDao.delete(id);
		}
	}

}
